package mysql

import (
	"gorm.io/gorm"
	"video-guard/videorpc/videorpc"
)

type CollectInfo struct {
	gorm.Model
	VideoId       int64 `gorm:"type:int(11);not null;comment: 视频ID"`
	UserId        int64 `gorm:"type:int(11);not null;comment:用户ID"`
	CollectStatus int   `gorm:"type:int(11);default:0;comment:收藏状态"`
}

func (i *CollectInfo) FindCollect(db *gorm.DB, uid int64, vid int64) error {
	return db.Where("collect_infos.user_id=?", uid).Where("collect_infos.video_id=?", vid).Find(i).Error
}

func (i *CollectInfo) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CollectInfo) CollectDel(db *gorm.DB) error {
	return db.Delete(i).Error
}

func CollectList(db *gorm.DB, in *videorpc.CollectListTRequest) []*videorpc.CollectList {
	var collect []*videorpc.CollectList
	db.Debug().Model(&CollectInfo{}).Select("video_infos.*",
		"user_infos.nick_name",
		"user_infos.avatar_link").Joins("LEFT JOIN user_infos ON collect_infos.user_id = user_infos.id").
		Joins("LEFT JOIN video_infos on video_infos.id = collect_infos.video_id").Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&collect)

	return collect
}
